//
//  HTemotionKeyboardViewCell.swift
//  表情键盘
//
//  Created by whtapple on 4/3/16.
//  Copyright © 2016 whtapple. All rights reserved.
//

import UIKit
//每页的 button数量
let pageemotionCount = 20
//每行 button数量
let HTEmoticonColumnOfPage = 7
//有多少行
let HTEmoticonRowOfPage = 3
//cell代理，将 button 模型传出去让键盘在 View上显示
protocol emotionKeyboardViewCellDelegate: NSObjectProtocol{
    //点击图标按钮代理
    func emtionKeyboardViewCell(cell: HTemotionKeyboardViewCell, didSelectedModel model: HTEmotionModel)
     //点击删除按钮代理
    func didClickDeleteButton()
}
class HTemotionKeyboardViewCell: UICollectionViewCell {
    //MARK: -属性
    //代理属性
    weak var delegate: emotionKeyboardViewCellDelegate?
    //创建一个表情数组模型，来接收每个 cell里面的所有表情模型
    var emotionsModels: [HTEmotionModel]?{
        didSet{
            //守卫一下，看看模型有没有拿到数据
            guard let emotionsM = emotionsModels else{
                print("没有拿到数据")
                return
            }
            //在将表情模型添加到按钮之前，先将 cell中的所有按钮都隐藏，防止复用按钮显示复用的图片
            for btn in emotions{
                btn.hidden = true
            }
            //遍历模型
            for (index,model) in emotionsM.enumerate(){
                //拿到对应下标的按钮
                let emotionsbtn = emotions[index]
                //给按钮的模型赋值
                emotionsbtn.model = model
                //将此按钮隐藏关闭
                emotionsbtn.hidden = false
            }
        }
    }
    var emotions: [HTemotionBtn] = [HTemotionBtn]()
    //第几行第几列属性
    var index: NSIndexPath?{
        didSet{
            label.text = "这是第\(index!.section)组，第\(index!.item)个"
        }
    }
    //MARK: -初始化
    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        prepareUI()
        contentView.addSubview(deletBtn)
        setBottomLabel()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -准备 UI
    private func prepareUI(){
        for _ in 0..<20{
            let btn: HTemotionBtn = HTemotionBtn()
            btn.addTarget(self, action: "didClickButton:", forControlEvents: UIControlEvents.TouchUpInside)
            contentView.addSubview(btn)
            emotions.append(btn)
        }
    }
    //MARK: -布局
    override func layoutSubviews() {
        //一定注意要调用父类的方法
        super.layoutSubviews()
        //进行布局
        relayoutsubviews()
    }
  //布局子控件方法
    func relayoutsubviews(){
        let margin: CGFloat = 3
        let bottomMargin: CGFloat = 26
        let width = (frame.width - 2 * margin) / CGFloat(HTEmoticonColumnOfPage)
        let height = (frame.height - bottomMargin) / CGFloat(HTEmoticonRowOfPage)
        //enumerate返回的就是一个带下标和按钮的元祖，在前面直接写出它的模型后面可以直接拿到
        for (index,button) in contentView.subviews.enumerate(){
            let buttonX = margin + CGFloat(index % HTEmoticonColumnOfPage) * width
            let buttonY = CGFloat(index / HTEmoticonColumnOfPage) * height
            button.frame = CGRect(x: buttonX, y: buttonY, width: width, height: height)
        }
    }
    //MARK: -添加 bottomLabel
    private func setBottomLabel(){
        addSubview(bottomLabel)
        bottomLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: bottomLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: bottomLabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -6))
    }
    //MARK: -按钮点击事件方法
    //点击button回调方法
    func didClickButton(button: HTemotionBtn){
        let model = button.model
        delegate?.emtionKeyboardViewCell(self, didSelectedModel: model!)
    }
    //点击删除按钮回调方法
    func didClickDeleteButton(){
        delegate?.didClickDeleteButton()
    }
    //MARK: -懒加载
    //懒加载label
    private lazy var label: UILabel = {
        let label: UILabel = UILabel()
        label.frame = CGRect(x: 0, y: 0, width: 300, height: 170)
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        self.addSubview(label)
        return label
    }()
    //懒加载删除按钮
    private lazy var deletBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "didClickDeleteButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //懒加载label
    private lazy var bottomLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.lightGrayColor()
        label.text = "I still love you so much,but I dont match it"
        return label
    }()
}
